package com.jk.controller.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.jk.util.SessionUtil;

/**
 * 对登录session 进行拦截
 * @author cuiP
 *
 */
public class SessionInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		String uri = request.getRequestURI();
		if(uri.indexOf("/admin/login/") == -1){
			if(session.getAttribute(SessionUtil.getSessionAdmin())==null){
				response.setContentType("text/html; charset=utf-8");
	            PrintWriter out = response.getWriter();  
	            StringBuilder builder = new StringBuilder();  
	            builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
	            builder.append("alert(\"登录超时,请从新登录!\");");  
	            builder.append("window.location.href=");  
	            builder.append("'"+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/admin/login/toLogin.do'");
	            builder.append("</script>");
	            out.print(builder.toString());  
	            out.close();  
	            return false;
			}
		}
		return true;
	}
}
